CMAKE_MINIMUM_REQUIRED(VERSION 3.17)

SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_C_COMPILER_WORKS TRUE)
SET(CMAKE_CXX_COMPILER_WORKS TRUE)

# select the correct path
IF (WIN32)
    SET(SUPPORT_NDK_PLATFORM_SYMB windows-x86_64)
    SET(SUPPORT_NDK_PLATFORM_SUFFIX .exe)
ELSE ()
    SET(SUPPORT_NDK_PLATFORM_SYMB linux-x86_64)
ENDIF ()

# setting compiler
SET(CMAKE_C_COMPILER ${SUPPORT_NDK}/toolchains/llvm/prebuilt/${SUPPORT_NDK_PLATFORM_SYMB}/bin/clang${SUPPORT_NDK_PLATFORM_SUFFIX})
SET(CMAKE_CXX_COMPILER ${SUPPORT_NDK}/toolchains/llvm/prebuilt/${SUPPORT_NDK_PLATFORM_SYMB}/bin/clang++${SUPPORT_NDK_PLATFORM_SUFFIX})

SET(SUPPORT_NDK_COMPILE_OPTIONS
        --target=armv7a-none-linux-android24
        --sysroot=${SUPPORT_NDK}/toolchains/llvm/prebuilt/${SUPPORT_NDK_PLATFORM_SYMB}/sysroot
        -fPIC
        -Werror
        -Wall
        -Wno-format-security)

SET(SUPPORT_NDK_COMPILE_OPTIONS_RELEASE ${SUPPORT_NDK_COMPILE_OPTIONS} -O0)
SET(SUPPORT_NDK_COMPILE_OPTIONS_DEBUG ${SUPPORT_NDK_COMPILE_OPTIONS} -O3)

# setting headers
SET(SUPPORT_NDK_COMPILE_INCLUDE
        ${SUPPORT_NDK}/toolchains/llvm/prebuilt/${SUPPORT_NDK_PLATFORM_SYMB}/sysroot/usr/include
        ${SUPPORT_NDK}/toolchains/llvm/prebuilt/${SUPPORT_NDK_PLATFORM_SYMB}/sysroot/usr/include/c++/v1)

IF (NOT TARGET NDK::ANDROID::LIBCXX::ABI)
    ADD_LIBRARY(NDK::ANDROID::LIBCXX::ABI STATIC IMPORTED)
    SET_TARGET_PROPERTIES(NDK::ANDROID::LIBCXX::ABI PROPERTIES IMPORTED_LOCATION "libc++abi.a")
ENDIF ()

IF (NOT TARGET NDK::ANDROID::LIBCXX::STATIC)
    ADD_LIBRARY(NDK::ANDROID::LIBCXX::STATIC STATIC IMPORTED)
    SET_TARGET_PROPERTIES(NDK::ANDROID::LIBCXX::STATIC PROPERTIES IMPORTED_LOCATION "libc++_static.a")
ENDIF ()

IF (NOT TARGET NDK::ANDROID::SUPPORT)
    ADD_LIBRARY(NDK::ANDROID::SUPPORT STATIC IMPORTED)
    SET_TARGET_PROPERTIES(NDK::ANDROID::SUPPORT PROPERTIES IMPORTED_LOCATION "libandroid_support.a")
ENDIF ()

IF (NOT TARGET NDK::ANDROID::Z)
    ADD_LIBRARY(NDK::ANDROID::Z INTERFACE IMPORTED)
    SET_TARGET_PROPERTIES(NDK::ANDROID::Z PROPERTIES IMPORTED_LOCATION "libz.so")
ENDIF ()

IF (NOT TARGET NDK::ANDROID::LIBUNWIND)
    ADD_LIBRARY(NDK::ANDROID::LIBUNWIND STATIC IMPORTED)
    SET_TARGET_PROPERTIES(NDK::ANDROID::LIBUNWIND PROPERTIES IMPORTED_LOCATION "libunwind.a")
ENDIF ()

IF (NOT TARGET NDK::ANDROID::LIBLOG)
    ADD_LIBRARY(NDK::ANDROID::LIBLOG INTERFACE IMPORTED)
    SET_TARGET_PROPERTIES(NDK::ANDROID::LIBLOG PROPERTIES IMPORTED_LOCATION "liblog.so")
ENDIF ()

IF (NOT TARGET NDK::ANDROID::LIBCAMERA2NDK)
    ADD_LIBRARY(NDK::ANDROID::LIBCAMERA2NDK INTERFACE IMPORTED)
    SET_TARGET_PROPERTIES(NDK::ANDROID::LIBCAMERA2NDK PROPERTIES IMPORTED_LOCATION "libcamera2ndk.so")
ENDIF ()

IF (NOT TARGET NDK::ANDROID::LIBANDROID)
    ADD_LIBRARY(NDK::ANDROID::LIBANDROID INTERFACE IMPORTED)
    SET_TARGET_PROPERTIES(NDK::ANDROID::LIBANDROID PROPERTIES IMPORTED_LOCATION "libandroid.so")
ENDIF ()

IF (NOT TARGET NDK::ANDROID::LIBMEDIANDK)
    ADD_LIBRARY(NDK::ANDROID::LIBMEDIANDK INTERFACE IMPORTED)
    SET_TARGET_PROPERTIES(NDK::ANDROID::LIBMEDIANDK PROPERTIES IMPORTED_LOCATION "libmediandk.so")
ENDIF ()

IF (NOT TARGET NDK::ANDROID::LIBGLESV3)
    ADD_LIBRARY(NDK::ANDROID::LIBGLESV3 INTERFACE IMPORTED)
    SET_TARGET_PROPERTIES(NDK::ANDROID::LIBGLESV3 PROPERTIES IMPORTED_LOCATION "libGLESv3.so")
ENDIF ()

IF (NOT TARGET LIBRARIES::ANDROID::RELEASE)
    ADD_LIBRARY(LIBRARIES::ANDROID::RELEASE INTERFACE IMPORTED)
    TARGET_COMPILE_OPTIONS(LIBRARIES::ANDROID::RELEASE INTERFACE "${SUPPORT_NDK_COMPILE_OPTIONS_RELEASE}")
    TARGET_INCLUDE_DIRECTORIES(LIBRARIES::ANDROID::RELEASE INTERFACE "${SUPPORT_NDK_COMPILE_INCLUDE}")
    TARGET_LINK_LIBRARIES(LIBRARIES::ANDROID::RELEASE INTERFACE
            NDK::ANDROID::LIBCXX::ABI
            NDK::ANDROID::LIBCXX::STATIC
            NDK::ANDROID::SUPPORT
            NDK::ANDROID::Z
            NDK::ANDROID::LIBUNWIND
            NDK::ANDROID::LIBLOG
            NDK::ANDROID::LIBCAMERA2NDK
            NDK::ANDROID::LIBANDROID
            NDK::ANDROID::LIBMEDIANDK
            NDK::ANDROID::LIBGLESV3)
ENDIF ()

IF (NOT TARGET LIBRARIES::ANDROID::DEBUG)
    ADD_LIBRARY(LIBRARIES::ANDROID::DEBUG INTERFACE IMPORTED)
    TARGET_COMPILE_OPTIONS(LIBRARIES::ANDROID::DEBUG INTERFACE "${SUPPORT_NDK_COMPILE_OPTIONS_DEBUG}")
    TARGET_INCLUDE_DIRECTORIES(LIBRARIES::ANDROID::DEBUG INTERFACE "${SUPPORT_NDK_COMPILE_INCLUDE}")
    TARGET_LINK_LIBRARIES(LIBRARIES::ANDROID::DEBUG INTERFACE
            NDK::ANDROID::LIBCXX::ABI
            NDK::ANDROID::LIBCXX::STATIC
            NDK::ANDROID::SUPPORT
            NDK::ANDROID::Z
            NDK::ANDROID::LIBUNWIND
            NDK::ANDROID::LIBLOG
            NDK::ANDROID::LIBCAMERA2NDK
            NDK::ANDROID::LIBANDROID
            NDK::ANDROID::LIBMEDIANDK
            NDK::ANDROID::LIBGLESV3)
ENDIF ()

SET(SUPPORT_NDK_COMPILE_LDFLAGS
        ${SUPPORT_NDK_COMPILE_OPTIONS}
        -nostdlib++
        --sysroot=${SUPPORT_NDK}/toolchains/llvm/prebuilt/${SUPPORT_NDK_PLATFORM_SYMB}/sysroot
        -fuse-ld=${SUPPORT_NDK}/toolchains/llvm/prebuilt/${SUPPORT_NDK_PLATFORM_SYMB}/bin/ld.lld)

FUNCTION(ANDROID_NDK_EXECUTE_BUILDXX TARGET_NAME OUT_NAME)
    IF (ARGC GREATER 2)
        MATH(EXPR LENGTH "${ARGC} - 1")
        FOREACH (INDEX RANGE 2 ${LENGTH} 1)
            GET_TARGET_PROPERTY(SELF_TYPE ${ARGV${INDEX}} TYPE)
            IF (SELF_TYPE MATCHES "OBJECT")
                CONTINUE()
            ENDIF ()

            GET_TARGET_PROPERTY(SELF_TEMP ${ARGV${INDEX}} INTERFACE_LINK_LIBRARIES)
            FOREACH (SELF_TEMP_ITEM ${SELF_TEMP})
                GET_TARGET_PROPERTY(SELF_TEMP_ITEM_LIB ${SELF_TEMP_ITEM} IMPORTED_LOCATION)
                IF (SELF_TEMP_ITEM_LIB MATCHES "-NOTFOUND")
                    CONTINUE()
                ENDIF ()

                GET_FILENAME_COMPONENT(SELF_TEMP_ITEM_BASE_PATH ${SELF_TEMP_ITEM_LIB} DIRECTORY)
                GET_FILENAME_COMPONENT(SELF_TEMP_ITEM_BASE_NAME ${SELF_TEMP_ITEM_LIB} NAME)
                IF (NOT SELF_TEMP_ITEM_BASE_PATH STREQUAL "")
                    STRING(APPEND SUPPORT_NDK_LIBS "-L${SELF_TEMP_ITEM_BASE_PATH}\t")
                    STRING(APPEND SUPPORT_NDK_LIBS "-l:${SELF_TEMP_ITEM_BASE_NAME}\t")
                ELSE ()
                    STRING(APPEND SUPPORT_NDK_LIBS "-l:${SELF_TEMP_ITEM_LIB}\t")
                ENDIF ()
            ENDFOREACH ()

            GET_TARGET_PROPERTY(SELF_TEMP ${ARGV${INDEX}} IMPORTED_LOCATION)
            IF (SELF_TEMP MATCHES "-NOTFOUND")
                CONTINUE()
            ENDIF ()

            STRING(APPEND SUPPORT_NDK_LIBS "${SELF_TEMP}\t")
        ENDFOREACH ()
    ENDIF ()

    ADD_CUSTOM_COMMAND(TARGET ${TARGET_NAME}
            COMMAND ${CMAKE_CXX_COMPILER} ${SUPPORT_NDK_COMPILE_LDFLAGS} -Wl,--no-undefined -O3 -o $<TARGET_FILE_DIR:${TARGET_NAME}>/${OUT_NAME} $<JOIN:$<TARGET_OBJECTS:${TARGET_NAME}>,\t> ${SUPPORT_NDK_LIBS}
            COMMAND ${SUPPORT_NDK}/toolchains/llvm/prebuilt/${SUPPORT_NDK_PLATFORM_SYMB}/bin/llvm-strip $<TARGET_FILE_DIR:${TARGET_NAME}>/${OUT_NAME}
            COMMENT "Link executor: ${OUT_NAME}")
ENDFUNCTION()

FUNCTION(ANDROID_NDK_SHARED_BUILDXX TARGET_NAME OUT_NAME)
    IF (ARGC GREATER 2)
        MATH(EXPR LENGTH "${ARGC} - 1")
        FOREACH (INDEX RANGE 2 ${LENGTH} 1)
            GET_TARGET_PROPERTY(SELF_TYPE ${ARGV${INDEX}} TYPE)
            IF (SELF_TYPE MATCHES "OBJECT")
                CONTINUE()
            ENDIF ()

            GET_TARGET_PROPERTY(SELF_TEMP ${ARGV${INDEX}} INTERFACE_LINK_LIBRARIES)
            FOREACH (SELF_TEMP_ITEM ${SELF_TEMP})
                GET_TARGET_PROPERTY(SELF_TEMP_ITEM_LIB ${SELF_TEMP_ITEM} IMPORTED_LOCATION)
                IF (SELF_TEMP_ITEM_LIB MATCHES "-NOTFOUND")
                    CONTINUE()
                ENDIF ()

                GET_FILENAME_COMPONENT(SELF_TEMP_ITEM_BASE_PATH ${SELF_TEMP_ITEM_LIB} DIRECTORY)
                GET_FILENAME_COMPONENT(SELF_TEMP_ITEM_BASE_NAME ${SELF_TEMP_ITEM_LIB} NAME)
                IF (NOT SELF_TEMP_ITEM_BASE_PATH STREQUAL "")
                    STRING(APPEND SUPPORT_NDK_LIBS "-L${SELF_TEMP_ITEM_BASE_PATH}\t")
                    STRING(APPEND SUPPORT_NDK_LIBS "-l:${SELF_TEMP_ITEM_BASE_NAME}\t")
                ELSE ()
                    STRING(APPEND SUPPORT_NDK_LIBS "-l:${SELF_TEMP_ITEM_LIB}\t")
                ENDIF ()
            ENDFOREACH ()

            GET_TARGET_PROPERTY(SELF_TEMP ${ARGV${INDEX}} IMPORTED_LOCATION)
            IF (SELF_TEMP MATCHES "-NOTFOUND")
                CONTINUE()
            ENDIF ()

            STRING(APPEND SUPPORT_NDK_LIBS "${SELF_TEMP}\t")
        ENDFOREACH ()
    ENDIF ()

    ADD_CUSTOM_COMMAND(TARGET ${TARGET_NAME}
            COMMAND ${CMAKE_CXX_COMPILER} -shared ${SUPPORT_NDK_COMPILE_LDFLAGS} -fvisibility=hidden -Wl,--no-undefined -Wl,--soname=lib${OUT_NAME}.so -O3 -o $<TARGET_FILE_DIR:${TARGET_NAME}>/lib${OUT_NAME}.so $<JOIN:$<TARGET_OBJECTS:${TARGET_NAME}>,\t> ${SUPPORT_NDK_LIBS}
            COMMAND ${SUPPORT_NDK}/toolchains/llvm/prebuilt/${SUPPORT_NDK_PLATFORM_SYMB}/bin/llvm-strip $<TARGET_FILE_DIR:${TARGET_NAME}>/lib${OUT_NAME}.so
            COMMENT "Link shared library: lib${OUT_NAME}.so")
ENDFUNCTION()

FUNCTION(ANDROID_NDK_SHARED_DEBUG_BUILDXX TARGET_NAME OUT_NAME)
    IF (ARGC GREATER 2)
        MATH(EXPR LENGTH "${ARGC} - 1")
        FOREACH (INDEX RANGE 2 ${LENGTH} 1)
            GET_TARGET_PROPERTY(SELF_TYPE ${ARGV${INDEX}} TYPE)
            IF (SELF_TYPE MATCHES "OBJECT")
                CONTINUE()
            ENDIF ()

            GET_TARGET_PROPERTY(SELF_TEMP ${ARGV${INDEX}} INTERFACE_LINK_LIBRARIES)
            FOREACH (SELF_TEMP_ITEM ${SELF_TEMP})
                GET_TARGET_PROPERTY(SELF_TEMP_ITEM_LIB ${SELF_TEMP_ITEM} IMPORTED_LOCATION)
                IF (SELF_TEMP_ITEM_LIB MATCHES "-NOTFOUND")
                    CONTINUE()
                ENDIF ()

                GET_FILENAME_COMPONENT(SELF_TEMP_ITEM_BASE_PATH ${SELF_TEMP_ITEM_LIB} DIRECTORY)
                GET_FILENAME_COMPONENT(SELF_TEMP_ITEM_BASE_NAME ${SELF_TEMP_ITEM_LIB} NAME)
                IF (NOT SELF_TEMP_ITEM_BASE_PATH STREQUAL "")
                    STRING(APPEND SUPPORT_NDK_LIBS "-L${SELF_TEMP_ITEM_BASE_PATH}\t")
                    STRING(APPEND SUPPORT_NDK_LIBS "-l:${SELF_TEMP_ITEM_BASE_NAME}\t")
                ELSE ()
                    STRING(APPEND SUPPORT_NDK_LIBS "-l:${SELF_TEMP_ITEM_LIB}\t")
                ENDIF ()
            ENDFOREACH ()

            GET_TARGET_PROPERTY(SELF_TEMP ${ARGV${INDEX}} IMPORTED_LOCATION)
            IF (SELF_TEMP MATCHES "-NOTFOUND")
                CONTINUE()
            ENDIF ()

            STRING(APPEND SUPPORT_NDK_LIBS "${SELF_TEMP}\t")
        ENDFOREACH ()
    ENDIF ()

    ADD_CUSTOM_COMMAND(TARGET ${TARGET_NAME}
            COMMAND ${CMAKE_CXX_COMPILER} -shared ${SUPPORT_NDK_COMPILE_LDFLAGS} -Wl,--no-undefined -Wl,--soname=lib${OUT_NAME}Debug.so -O0 -o $<TARGET_FILE_DIR:${TARGET_NAME}>/lib${OUT_NAME}Debug.so $<JOIN:$<TARGET_OBJECTS:${TARGET_NAME}>,\t> ${SUPPORT_NDK_LIBS}
            COMMENT "Link debug shared library: lib${OUT_NAME}Debug.so")
ENDFUNCTION()
